/* 
 * Copyright 2009 Shared Object (www.sharedobject.ch)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package edos.prm.engine;

import java.lang.reflect.Method;

import edos.prm.activity.ActivityManager;
import edos.prm.activity.ActivityManager.PrmActivityManager;
import edos.prm.actor.ActorManager;
import edos.prm.actor.ActorManager.PrmActorManager;
import edos.prm.artifact.ArtifactManager;
import edos.prm.artifact.ArtifactManager.PrmArtifactManager;
import edos.prm.event.EventManager;
import edos.prm.event.EventManager.PrmEventManager;
import edos.prm.metrics.MetricsManager;
import edos.prm.metrics.MetricsManager.PrmMetricsManager;
import edos.prm.process.ProcessManager;
import edos.prm.process.ProcessManager.PrmProcessManager;
import edos.prm.project.ProjectManager;
import edos.prm.project.ProjectManager.PrmProjectManager;
import edos.prm.role.RoleManager;
import edos.prm.role.RoleManager.PrmRoleManager;
import edos.prm.task.TaskManager;
import edos.prm.task.TaskManager.PrmTaskManager;
import edos.prm.util.VersionNumber;

/**
 * This class is where the PRM starts executing.
 * <p>
 * For the moment, the PRM links to the default implementation of the main
 * activities.
 */
public final class PRM implements Runnable {

	private String programName;
	private static String programArgument;
	
	private static ArtifactManager artifactManager;
	private static ActorManager actorManager;
	private static EventManager eventManager;
	private static RoleManager roleManager;
	private static ProjectManager projectManager;
	private static ProcessManager processManager;
	private static TaskManager taskManager;
	private static ActivityManager activityManager;
	private static MetricsManager metricsManager;
	
	public PRM(String program) {
		this.programName = program;
	}

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		programArgument = args[0];
		// Provoke execution of task manager
		PrmTaskManager.kickoff();
	}

	public void run() {
		System.out.println("Ici le PRM .... ");
		
		// Set up the default task
		artifactManager = PrmArtifactManager.getInstance();
		actorManager = PrmActorManager.getInstance();
		eventManager = PrmEventManager.getInstance();
		roleManager = PrmRoleManager.getInstance();
		projectManager = PrmProjectManager.getInstance();
		processManager = PrmProcessManager.getInstance();
		taskManager = PrmTaskManager.getInstance();
		activityManager = PrmActivityManager.getInstance();
		metricsManager = PrmMetricsManager.getInstance();
		
		// The security manager has the engine that implements the
		// role mechanism
		System.setSecurityManager(new PrmSecurityManager());
		
		// Start the program
		try {
			Class<?> clazz = Class.forName(programName);
			Method mainMethod = clazz.getMethod("run");
			Object obj = clazz.newInstance();
			mainMethod.invoke(obj);
		} catch (Exception exc) {
			exc.printStackTrace();
		}
	}

	public static Object getProgramName() {
		return programArgument;
	}
	
	public static ArtifactManager getArtifactManager() {
		return artifactManager;
	}

	public static ActorManager getActorManager() {
		return actorManager;
	}

	public static EventManager getEventManager() {
		return eventManager;
	}

	public static RoleManager getRoleManager() {
		return roleManager;
	}
	
	public static ProjectManager getProjectManager() {
		return projectManager;
	}

	public static ProcessManager getProcessManager() {
		return processManager;
	}
	
	public static TaskManager getTaskManager() {
		return taskManager;
	}
	
	public static ActivityManager getActivityManager() {
		return activityManager;
	}
	
	public static MetricsManager getMetricsManager() {
		return metricsManager;
	}

	public static VersionNumber getPRMVersion() {
		return new VersionNumber("1.0.0");
	}
}
